import 'package:flutter/material.dart';

import 'dart:ui' as ui;

class SugarTabIndicator extends Decoration {
  final List<Color>? gradientColor;

  final BorderSide borderSide;

  final EdgeInsetsGeometry insets;

  const SugarTabIndicator(
      {this.borderSide = const BorderSide(width: 2.0, color: Colors.white),
      this.insets = EdgeInsets.zero,
      this.gradientColor})
      : assert(borderSide != null),
        assert(insets != null);


  @override
  _UnderlinePainter createBoxPainter([VoidCallback? onChanged]) {
    return _UnderlinePainter(this, onChanged!, gradientColor);
  }
}

class _UnderlinePainter extends BoxPainter {
  _UnderlinePainter(this.decoration, VoidCallback onChanged, this.gradientColor)
      : assert(decoration != null),
        super(onChanged);

  final SugarTabIndicator decoration;

  BorderSide get borderSide => decoration.borderSide;

  EdgeInsetsGeometry get insets => decoration.insets;

  List<Color>? gradientColor;

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    assert(configuration != null);

    assert(configuration.size != null);

    final Rect rect = offset & configuration.size!;

    final Paint paint = borderSide.toPaint()..strokeCap = StrokeCap.square;

    Rect myRect = Rect.fromLTWH(
        rect.left + rect.width / 4, rect.bottom - 7, rect.width / 2, 2);

    paint.shader = ui.Gradient.linear(
        Offset(myRect.left, 0), Offset(myRect.right, 0), gradientColor!);

    paint.strokeWidth = 3;

    canvas.drawRRect(
        RRect.fromRectAndRadius(myRect, Radius.circular(3)), paint);
  }
}
